﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calculate
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("输入算式：");
            string input = Console.ReadLine();
            StringBuilder StrData = new StringBuilder(); //数据及运算符保存 
            StrData.Append(input);
            CalcData1(StrData);
            Console.Read();
        }

        public static void CalcData1(StringBuilder StrData)
        {
            char[] SplitSymbol = new char[] { '+', '-' };
            string StrDataTemp = StrData.ToString(); ;
            string[] StrDataArr = StrDataTemp.Split(SplitSymbol);
            double ResultData = 0;
            int j = 0;

            //先以加减运算符为分隔符分割字符串，处理分割后数组中的乘除运算
            for (int i = 0; i < StrDataArr.Length; i++)
            {
                if (StrDataArr[i].Contains("*") || StrDataArr[i].Contains("/") || StrDataArr[i].Contains("%"))
                {
                    StrDataArr[i] = CalcData(StrDataArr[i]).ToString();
                }
                else if (StrDataArr[i] == "" || StrDataArr[i] == null)
                {
                    StrDataArr[i] = "0";
                }
            }

            //加减运算
            ResultData = Double.Parse(StrDataArr[0]);
            foreach (char temps in StrDataTemp)
            {
                switch (temps)
                {
                    case '-':
                        ResultData = ResultData - Double.Parse(StrDataArr[j + 1]);
                        j++;
                        break;
                    case '+':
                        ResultData = ResultData + Double.Parse(StrDataArr[j + 1]);
                        j++;
                        break;
                    default:
                        break;
                }
            }

            StrData.Clear();
            StrData.Append(ResultData);
            Console.Write(ResultData);
        }

        public static double CalcData(string s)
        {
            char[] splitor = { '*', '/', '%' };
            string[] strArr = s.Split(splitor);
            double tempData = 0;
            int i = 0;
            tempData = Double.Parse(strArr[0]);
            foreach (char temps in s)
            {
                switch (temps)
                {
                    case '/':
                        if (strArr[1] == "0")
                        {
                            Console.Write("除数不能为0!");
                            tempData = tempData / Double.Parse(strArr[i + 1]);
                        }
                        else
                        {
                            tempData = tempData / Double.Parse(strArr[i + 1]);
                        }
                        i++;
                        break;
                    case '*':
                        tempData = tempData * Double.Parse(strArr[i + 1]);
                        i++;
                        break;
                    case '%':
                        tempData = tempData % Double.Parse(strArr[i + 1]);
                        i++;
                        break;
                    default:
                        break;
                }
            }
            return tempData;
        }
    }
}
